package com.bawei.flink.adsstatic;

import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.CreateTableOptions;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;

import java.util.LinkedList;

public class CreateTable {

    public static void main(String[] args) throws KuduException {
        KuduClient kuduClient = new KuduClient.KuduClientBuilder("cdh1:7051")
                .defaultSocketReadTimeoutMs(60000)
                .build();


        LinkedList<ColumnSchema> columns = new LinkedList<>();
        columns.add(new ColumnSchema.ColumnSchemaBuilder("startTime", Type.STRING).key(true).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("endTime", Type.STRING).key(true).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("city", Type.STRING).key(true).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("adsId", Type.INT32).key(true).build());
        columns.add(new ColumnSchema.ColumnSchemaBuilder("cnts", Type.INT64).key(false).build());

        Schema schema = new Schema(columns);

        CreateTableOptions options = new CreateTableOptions();
        LinkedList<String> partitionBy = new LinkedList<>();
        partitionBy.add("city");
        partitionBy.add("adsId");
        options.addHashPartitions(partitionBy,3);
        options.setNumReplicas(1);

        kuduClient.createTable("ads_log",schema,options);

        kuduClient.close();
    }

}
